package top.wbhzx.redis;

import redis.clients.jedis.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class RedisClient {
    /** 非切片额客户端连接 */
    private Jedis jedis;

    /** 非切片连接池 */
    private JedisPool jedisPool;

    /** 切片额客户端连接 */
    private ShardedJedis shardedJedis;

    /** 切片连接池 */
    private ShardedJedisPool shardedJedisPool;

    public RedisClient() {
        initialPool();
        initialShardedPool();
        shardedJedis = shardedJedisPool.getResource();
        jedis = jedisPool.getResource();
    }

    /**
     * 初始化非切片池
     */
    private void initialPool() {
        /** 池基本配置 */
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(5);
        config.setMaxWaitMillis(10001);
        config.setTestOnBorrow(false);

        jedisPool = new JedisPool(config, "127.0.0.1", 6379);

    }

    /**
     * 初始化切片池
     */
    private void initialShardedPool() {
        /** 池基本配置 */
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(5);
        config.setMaxWaitMillis(10001);
        config.setTestOnBorrow(false);
        /** slave链接 */
        List<JedisShardInfo> shardInfos = new ArrayList<JedisShardInfo>();
        shardInfos.add(new JedisShardInfo("127.0.0.1", 6379, "master"));

        //构造器
        shardedJedisPool = new ShardedJedisPool(config, shardInfos);
    }

    public void show() {
        KeyOperate();
        StringOperate();
        ListOperate();
        SetOperate();
        SortecSetOperate();
        HashOpeate();
        jedisPool.returnBrokenResource(jedis);
        shardedJedisPool.returnBrokenResource(shardedJedis);

    }

    private void KeyOperate() {
        System.out.println("=======================key============================");
        // 清空数据
        System.out.println("清空库中所有数据：" + jedis.flushDB());
        // 判断key是否存在
        System.out.println("myKey是否存在：" + shardedJedis.exists("myKey"));
        System.out.println("新增myKey1,abc键值对：" + shardedJedis.set("myKey1", "abc"));
        System.out.println("判断mykey1是否存在：" + shardedJedis.exists("mykey1"));
        // 输入系统中所有的key
        System.out.println("新增myKey2,xyz键值对：" + shardedJedis.set("myKey2", "xyz"));
        System.out.println("系统中所有的键值对如下：");
        Set<String> keys = jedis.keys("*");
        Iterator<String> iterator = keys.iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            System.out.println(key);
        }
        // 删除某个key,若key不存在,则忽略
        System.out.println("系统中删除myKey2:" + jedis.del("myKey2"));
        System.out.println("判断myKey2是否存在：" + shardedJedis.exists("myKey2"));
        // 设置myKey1的过期时间
        System.out.println("设置myKey1的过期时间为5秒：" + jedis.expire("myKey1", 5));
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {

        }
        // 查看某个key的剩余生存时间【秒】，永久或不存在返回-1
        System.out.println("查看myKey1的剩余生存时间：" + jedis.ttl("myKey1"));
        // 移除某个key的剩余生存时间
        System.out.println("移除myKey1的剩余生存时间：" + jedis.persist("myKey1"));
        System.out.println("查看myKey1的剩余生存使时间：" + shardedJedis.exists("myKey1"));
        // 查看所有key存储的值类型
        System.out.println("查看mykey1储存的值类型：" + jedis.type("myKey1"));

        // 修改键名
        System.out.println("将myKey1修改为myKeyReName：" + jedis.rename("myKey1", "myKeyReName"));

        // 移动一个key到其他的DB
        // jedis.move("foo",1)
    }

    private void StringOperate() {
    }

    private void ListOperate() {
    }

    private void SetOperate() {
    }

    private void SortecSetOperate() {
    }

    private void HashOpeate() {
    }
}
